#include <Wt/WText>
#include <Wt/WVBoxLayout>
#include <Wt/WApplication>
#include <Wt/WComboBox>
#include <Wt/WContainerWidget>
#include <Wt/WHBoxLayout>

#include "DbSession.h"
#include <QVector>
#include <QString>
#include "OtherWidget.h"
#include "PersonalWidget.h"

OtherWidget::OtherWidget(int id, WApplication *app)
{
    UserId = id;
    users = DbSession::getInstance().getAllUsers();

    usersBox = new WComboBox();
    for (int i = 0; i < users->count(); i++)
    {
        if (users->at(i)->getId() != id)
        {
            usersBox->addItem(users->at(i)->getLogin().toStdString());
        }
    }
    usersBox->sactivated().connect(this, &OtherWidget::onActivated);

    //page = new PersonalWidget(UserId, app, true);
    page = 0;



    WHBoxLayout *usersLayout = new WHBoxLayout();
    usersLayout->addWidget(new WText("User:"));
    usersLayout->addWidget(usersBox);
    usersLayout->addStretch(1);
    //this->addWidget(usersLayout);

    layout = new WVBoxLayout();
    layout->addItem(usersLayout);
    layout->addStretch(1);
    this->setLayout(layout);

    if (usersBox->count() > 0)
    {
        onActivated(usersBox->currentText());
    }
}

OtherWidget::~OtherWidget()
{

}

void OtherWidget::onActivated(WString login)
{

    if (page != 0)
    {
        layout->removeWidget(page);
        //this->removeWidget(page);
        if (page)
        {
            //delete page;
        }
    }


    int id = 0;
    for (int i = 0; i < users->count(); i++)
    {
        if (login.toUTF8() == users->at(i)->getLogin().toStdString())
        {
            id = users->at(i)->getId();
        }
    }

    if (id)
    {
        page = new PersonalWidget(id, WApplication::instance(), true, this->UserId);
        layout->insertWidget(1, page);
        //this->addWidget(page);
    }
}
